<?php

namespace app\api\service;

use app\common\model\IntegralModel;
use app\common\model\SignModel;

class SignService extends CommonService
{
    public function sign(array $params){
        //判断用户是否签到
        $signModel = new SignModel();
        $where = ['group_id'=>$params['from_wxid'],'uid'=>$params['final_from_wxid']];
        $ret = $signModel->getSignToDayDataById($where);
        $integralModel = new IntegralModel();
        $res = $integralModel->getUserData($where);
        if($ret){
            $message = " \r 您今天已签到过~\r".
                "   总  积  分:".$res['sum_num']." \r".
                "   可用积分:".$res['num']. " \r".
                "----------------\r".
                "ps:积分可用于兑换技能";
          $this->send_group_at_msg($params['robot_wxid'],$params['from_wxid'],$params['final_from_wxid'],$params['final_from_name'],$message);
          exit();
        }
        //处理今天没有签到的
        $num = rand(1,3);
        if(!$res){
            $integralModel->insert([
                'group_id'=>$params['from_wxid'],
                'uid'=>$params['final_from_wxid'],
                'sum_num'=>$num,
                'num'=>$num,
            ]);
        }else{
            $integralModel->incNumber($where,$num);
        }
        $signModel->insertWxData($params);
        $res = $integralModel->getUserData($where);
        trace('写入后的积分情况');
        trace($res);
        $message = " \r 恭喜签到成功~\r".
            "   签到积分:".$num ." \r".
            "   总  积  分:".$res['sum_num']." \r".
            "   可用积分:".$res['num']. " \r".
            "----------------\r".
            "ps:积分可用于兑换技能";
        $this->send_group_at_msg($params['robot_wxid'],$params['from_wxid'],$params['final_from_wxid'],$params['final_from_name'],$message);

    }

}